/*
                    Imagebox Class Implementation
*/
#include "Imagebox.h"

Imagebox::Imagebox()
{
    Position = Point(0,0);

    Image = NULL;

    bVisible = true;
    bEnabled = true;
}

Imagebox::~Imagebox()
{
    if (Image != NULL) { SDL_FreeSurface(Image); }
}

int Imagebox::Initialise(string strFilepath, Point NewPosition)
{
    Image = OptimiseAlphaSurface(IMG_Load(strFilepath.c_str()));

    Position = NewPosition;

    return 0;
}

void Imagebox::setImage(string strFilepath)
{
    if (Image != NULL) { SDL_FreeSurface(Image); }

    Image = IMG_Load(strFilepath.c_str());
}

void Imagebox::setImage(SDL_Surface *NewImage)
{
    if (Image != NULL) { SDL_FreeSurface(Image); }

    Image = NewImage;
}

SDL_Surface* Imagebox::getImage()
{
    return Image;
}

void Imagebox::setPosition(Point NewPosition)
{
    Position = NewPosition;
}

void Imagebox::setPosition(double nx, double ny)
{
    Position.x = nx;
    Position.y = ny;
}

Point Imagebox::getPosition()
{
    return Position;
}

int Imagebox::MoveRelative(double nx, double ny)
{
    Position.x += nx;
    Position.y += ny;
}

int Imagebox::BlitToSurface(SDL_Surface *screen)
{
    if (bVisible == true)
    {
        if (Image == NULL)
        {
            return -1;
        }

        else
        {
            SDL_Rect dest;
            dest.x = static_cast<int>(Position.x);
            dest.y = static_cast<int>(Position.y);
            SDL_BlitSurface(Image, NULL, screen, &dest);

            return 0;
        }
    }
}

int Imagebox::BlitToFrame(SDL_Surface *surface, Point FramePosition)
{
    if (bVisible == true)
    {
        if (Image == NULL)
        {
            return -1;
        }
        else
        {
            SDL_Rect dest;
            dest.x = static_cast<int>(Position.x) + static_cast<int>(FramePosition.x);
            dest.y = static_cast<int>(Position.y) + static_cast<int>(FramePosition.y);
            SDL_BlitSurface(Image, NULL, surface, &dest);

            return 0;
        }
    }
}
